package com.daxia.bumall.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * ApiTest
 */
@Entity
@Table(name = "apitest") 
public class ApiTest extends BaseModel {
	/**
	 * id
	 */
	@Id
	@GeneratedValue
	@Column(name = "id")
    private Long id;
	/**
	 * api名称
	 */
	@Column(name = "name")
    private String name;
	/**
	 * 描述
	 */
	@Column(name = "description")
    private String description;
	/**
	 * url
	 */
	@Column(name = "url")
    private String url;
    @Transient
    private String fullUrl;
    @OneToMany(mappedBy = "apiTest", cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
    private List<ApiTestParameter> apiTestParameters;

	/**
	 * api模块
	 */
	@ManyToOne
	@JoinColumn(name = "apiModule_id")
    private ApiModule apiModule;
	/**
	 * 示例url
	 */
	@Column(name = "exampleUrl")
    private String exampleUrl;
	/**
	 * 示例响应
	 */
	@Column(name = "exampleResponse")
    private String exampleResponse;
	/**
	 * 请求方式
	 */
	@Column(name = "requestmethod")
    private String requestMethod;
	/**
	 * 状态，0: 有效，1：无效
	 */
	@Column(name = "status")
    private Integer status;
	/**
	 * 返回说明
	 */
	@Column(name = "responseRemark")
    private String responseRemark;
	
	@Column(name = "isOverrideResponse")
	private Boolean isOverrideResponse;
	
	public Long getId() {
    	return id;
    }
	
    public void setId(Long id) {
    	this.id = id;
    }
	
	public String getName() {
    	return name;
    }
	
    public void setName(String name) {
    	this.name = name;
    }
	
	public String getDescription() {
    	return description;
    }
	
    public void setDescription(String description) {
    	this.description = description;
    }
	
	public String getUrl() {
    	return url;
    }
	
    public void setUrl(String url) {
    	this.url = url;
    }
	
	public ApiModule getApiModule() {
    	return apiModule;
    }
	
    public void setApiModule(ApiModule apiModule) {
    	this.apiModule = apiModule;
    }
	
	public String getExampleUrl() {
    	return exampleUrl;
    }
	
    public void setExampleUrl(String exampleUrl) {
    	this.exampleUrl = exampleUrl;
    }
	
	public String getExampleResponse() {
    	return exampleResponse;
    }
	
    public void setExampleResponse(String exampleResponse) {
    	this.exampleResponse = exampleResponse;
    }
	
	public String getRequestMethod() {
    	return requestMethod;
    }
	
    public void setRequestMethod(String requestMethod) {
    	this.requestMethod = requestMethod;
    }
	
	public Integer getStatus() {
    	return status;
    }
	
    public void setStatus(Integer status) {
    	this.status = status;
    }
	
	public String getResponseRemark() {
    	return responseRemark;
    }
	
    public void setResponseRemark(String responseRemark) {
    	this.responseRemark = responseRemark;
    }

    public void setFullUrl(String fullUrl) {
        this.fullUrl = fullUrl;
    }

    public String getFullUrl() {
        List<ApiTestParameter> parameters = getApiTestParameters();
        if (CollectionUtils.isEmpty(parameters)) {
            return getUrl();
        }

        List<String> list = new ArrayList<String>();
        for (ApiTestParameter p : parameters) {
            list.add(p.getName() + "=");
        }
        return getUrl() + "?" + StringUtils.join(list, "&");
    }
    
    public List<ApiTestParameter> getApiTestParameters() {
        return apiTestParameters;
    }

    public void setApiTestParameters(List<ApiTestParameter> apiTestParameters) {
        this.apiTestParameters = apiTestParameters;
    }

	public Boolean getIsOverrideResponse() {
		return isOverrideResponse;
	}

	public void setIsOverrideResponse(Boolean isOverrideResponse) {
		this.isOverrideResponse = isOverrideResponse;
	}
    
}
